<?php

namespace App\Resource;

use App\Model\Collect;
use Hyperf\Resource\Json\ResourceCollection;

class CityCollection extends ResourceCollection
{
    /**
     * Transform the resource collection into an array.
     *
     * @return array
     */
    public function toArray(): array
    {
        return parent::toArray();
    }

    public function toListApi()
    {
        $list = $this->groupBy('initial');

        $data = [
            'hot' => [],
            'list' => [],
        ];
        foreach ($list as $k => $v) {
            $re = [
                'initial' => $k,
                'list' => [],
            ];
            foreach ($v as $city) {
                $re['list'][] = [
                    'id' => $city->id ?? '',
                    'name' => $city->name ?? '',
                ];

                if ($city->is_hot == 1) {
                    $data['hot'][] = [
                        'id' => $city->id ?? '',
                        'name' => $city->name ?? '',
                    ];
                }
            }
            $data['list'][] = $re;
        }

        return $data;
    }
}
